export function format(Date, str) {
  var obj = {
    Y: Date.getFullYear(),
    M: Date.getMonth() + 1,
    D: Date.getDate(),
    H: Date.getHours() < 10 ? "0" + Date.getHours() : Date.getHours(),
    Mi: Date.getMinutes() < 10 ? "0" + Date.getMinutes() : Date.getMinutes(),
    S: Date.getSeconds() < 10 ? "0" + Date.getSeconds() : Date.getSeconds(),
  }
  // 拼接时间 hh:mm:ss
  var time = ' ' + obj.H + ':' + obj.Mi + ":" + obj.S;
  // yyyy-mm-dd
  if(str.indexOf('-') > -1) {
    return obj.Y + '-' + obj.M + '-' + obj.D + time;
  }
  
  // yyyy/mm/dd
  if(str.indexOf('/') > -1) {
    return obj.Y + '/' +obj.M + '/' + obj.D + time;
  }
}

export function formaDate(date, format) {
  if(date != null && format != null && format != "") {
    var nowDate = new Date(date)
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate()
    var hours = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours()
    var minutes = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes()
    var seconds = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds()
    if(format.toUpperCase() == "YYYY-MM-DD") {
      return year + "-" + month + "-" + date;
    } else if(format.toUpperCase() == "YYYY-MM-DD HH:MM:SS") {
      return year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds
    } else if(format.toUpperCase() == "HH:MM:SS") {
      return hours + ":" + minutes + ":" + seconds
    } else if (format.toUpperCase() == "YYYY-MM-DD HH") {
      return year + "-" + month + "-" + date + " " + hours;
    }
  } else {
    return null;
  }
}


export function formatYMD(time) {
  let date = new Date(time)
  let Y = date.getFullYear()
  let M =  date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  
  return Y + '/' + M + '/' + D
}

